<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('categoryFieldSearch')) {

    function categoryFieldSearch($searchData = array())
    {
        $CI = & get_instance();
        $loading = $CI->lang->line('Loading');
        $_field_search_attribute = array(
            'textsearch' => array('name' => 'textsearch', 'id' => 'frm-textsearch',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_keyword'),
                'value' => set_value('textsearch', isset($searchData['textsearch']) ? $searchData['textsearch'] : '')
            ),
            'submit' => array('name' => 'btn-Search', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnSearch', 'value' => $CI->lang->line('Search')),
            'reset' => array('name' => 'btn-Reset', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnReset', 'value' => $CI->lang->line('Reset')),
        );

        return $_field_search_attribute;
    }

}

//define field to generate form
if (!function_exists('categoryFieldForm')) {

    function categoryFieldForm($category = array(), $tabs = array())
    {
        $CI = & get_instance();
        if (count($category) && !empty($category)) {
            $name = $category->name;
            $active = $category->active;
            $order = $category->order;
            $selectedParentId = $category->parent_id;
        }

        $_field_form = array(
            'name' => array('name' => 'name', 'id' => 'frm-name',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_category'),
                'value' => set_value('name', isset($name) ? $name : '')),
            'parent_id' => array(
                'name' => 'parent_id',
                'options' => $tabs,
                'selected' => isset($selectedParentId) ? array($selectedParentId) : array(),
            ),
            'active' => array(
                'name' => 'active',
                'class' => 'class="checkActive"',
                'value' => 1,
                'checked' => (isset($active) && $active > 0) ? TRUE : FALSE
            ),
            'order' => array('name' => 'order', 'id' => 'frm-order',
                'class' => 'span3',
                'value' => set_value('order', isset($order) ? $order : 0)),
            'submit' => array('class' => 'btn btn-primary', 'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-submit', 'value' => $CI->lang->line('Save')),
        );

        return $_field_form;
    }

}

//define field to validate form
if (!function_exists('categoryFieldValidate')) {

    function categoryFieldValidate()
    {
        $CI = & get_instance();
        $_field_validate = array(
            array(
                'field' => 'name',
                'label' => $CI->lang->line('Question_Text'),
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'order',
                'label' => $CI->lang->line('Order'),
                'rules' => 'trim|is_natural|xss_clean'
            ),
        );
        return $_field_validate;
    }

}
